//
//  JGRegistContentView.m
//  JGRider
//
//  Created by 单启志 on 2018/3/2.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGRegistContentView.h"
#import "FQCountDownButton.h"
#import <MOFSPickerManager.h>
//#import "JGValidateCodeApi.h"
#import "FQGetValidateCodeHelper.h"
#import "JGGetDistrictApi.h"
#import "JGCityCodeHelper.h"
@interface JGRegistContentView()<YTKChainRequestDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTF;//姓名
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;//手机号
@property (weak, nonatomic) IBOutlet UITextField *validateCodeTF;//验证码
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;//密码
@property (weak, nonatomic) IBOutlet FQCountDownButton *validateCodeBtn;//验证码btn

@property (weak, nonatomic) IBOutlet UIView *provinceSuperView;//省边框
@property (weak, nonatomic) IBOutlet UILabel *provinceLab;//省
@property (weak, nonatomic) IBOutlet UIView *citySuperView;//市区边框
@property (weak, nonatomic) IBOutlet UILabel *cityLab;//市区


@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;//确定

//系统返回的验证码
@property(nonatomic,copy)NSString *realValidateCode;
@property(nonatomic,strong)MBProgressHUD *hud;

@property(nonatomic,copy)NSString *provinceId;
@property(nonatomic,copy)NSString *cityId;
@property(nonatomic,copy)NSString *contyId;
@end
@implementation JGRegistContentView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
        self.frame=frame;
        [self layoutSubViews];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
}

-(void)layoutSubViews{
    self.confirmBtn.layer.cornerRadius=AdaptedWidth(3);
    self.confirmBtn.layer.masksToBounds=YES;
    self.validateCodeBtn.layer.cornerRadius=AdaptedWidth(5);
    self.validateCodeBtn.layer.masksToBounds=YES;
    [self.validateCodeBtn setTitleColor:HEXCOLOR(0xfea129) forState:UIControlStateNormal];
    [self.validateCodeBtn setTitleColor:HEXCOLOR(0xffffff) forState:UIControlStateDisabled];
   
    self.validateCodeBtn.layer.borderColor=HEXCOLOR(0xfea129).CGColor;
    self.validateCodeBtn.layer.borderWidth=AdaptedHeight(0.5);
    
    self.provinceSuperView.layer.borderColor=HEXCOLOR(0xdbdbdb).CGColor;
    self.provinceSuperView.layer.borderWidth=AdaptedWidth(0.5);
    self.citySuperView.layer.borderColor=HEXCOLOR(0xdbdbdb).CGColor;
    self.citySuperView.layer.borderWidth=AdaptedWidth(0.5);
}

//获取验证码
- (IBAction)onValidateCodeBtnClick:(FQCountDownButton*)sender {

    FQWeakSelf(self);
    [[FQGetValidateCodeHelper share]getValidateCodeWithMobile:self.phoneTF.text  view:self button:sender sucess:^(NSString *code) {
        weakself.realValidateCode=code;
    } faile:nil];

//    if (FQIsEmpty(self.phoneTF.text)) {
//        [MBProgressHUD showError:@"请先输入手机号！" ToView:self];
//        return;
//    }
//
//   _hud=[MBProgressHUD showMessage:@"获取中..." ToView:self];
//    FQGetSignApi *signApi=[[FQGetSignApi alloc]initWithParams:[NSDictionary dictionaryWithObject:self.phoneTF.text forKey:@"mobile"]];
//    FQChainRequest *chainReq=[[FQChainRequest alloc]init];
//    [chainReq addRequest:signApi callback:^(YTKChainRequest * _Nonnull chainRequest, YTKBaseRequest * _Nonnull baseRequest) {
//        FQGetSignApi *req=(FQGetSignApi*)baseRequest;
//        JGValidateCodeApi *api=[[JGValidateCodeApi alloc]initWithSi:[req si] Pa:[req pa]];
//        [chainRequest addRequest:api callback:nil];
//    }];
//    chainReq.delegate=self;
//    [chainReq start];

    if ([self.delegate respondsToSelector:@selector(contentViewValidateCodeEvent)]) {
        [self.delegate contentViewValidateCodeEvent];
    }
    
}

//选择区域
- (IBAction)onChooseAddressBtnClick:(id)sender {
//    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
//    dic[@"parentId"]=@(0);
//    [[JGRequestHelper share]requestWithParams:nil requestApi:[JGGetDistrictApi class] hudMessage:@"" view:self successBlock:^(id result) {
//        [[JGCodingHelper share]writeCityCodeToPlistWithData:result[@"data"][@"list"]];
//    } faileBlock:nil];

    [[MOFSPickerManager shareManger]showMOFSAddressPickerWithTitle:@"请选择接单区域" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *address, NSString *zipcode) {
        self.provinceLab.text=[address substringToIndex:3];
        self.cityLab.text=[address substringFromIndex:4];
    } cancelBlock:^{
        
    }];
    
}


//确定
- (IBAction)onConfirmBtnClick:(id)sender {

    if ([self validateInput]==NO) {
        return;
    }
    if ([self.delegate respondsToSelector:@selector(contentViewConfirmEventWithParams:)]) {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        dic[@"roleId"]=roleId;
        dic[@"nickName"]=[self.nameTF.text stringByTrim];
        dic[@"account"]=[self.phoneTF.text stringByTrim];
        dic[@"mobileCode"]=[self.validateCodeTF.text stringByTrim];
        dic[@"pwd"]=[self.pwdTF.text stringByTrim];
        dic[@"provinceId"]=[self.provinceLab.text stringByTrim];
        dic[@"cityId"]=[[self.cityLab.text substringToIndex:3]stringByTrim];
        dic[@"contyId"]=[[self.cityLab.text substringFromIndex:4]stringByTrim];


        dic[@"address"]=@"荔湾区周门北路荔天大厦505";
        [self.delegate contentViewConfirmEventWithParams:dic];
    }
}
#pragma mark - 验证输入框合法性
//验证输入内容
-(BOOL)validateInput{
    if ([self validateWithFT:self.nameTF withMessage:@"请输入姓名"]==NO) {
        return NO;
    }
    if ([self validateWithFT:self.phoneTF withMessage:@"请输入手机号码"]==NO) {
        return NO;
    }
    if ([self validateWithFT:self.validateCodeTF withMessage:@"请输入验证码"]==NO) {
        return NO;
    }
    if ([self validateCode]==NO) {
        return NO;
    }
    if ([self validateWithFT:self.pwdTF withMessage:@"请输入密码"]==NO) {
        return NO;
    }
    if ([self validatePwdFormat]==NO) {
        return NO;
    }
    return YES;

}

//验证输入框非空
-(BOOL)validateWithFT:(UITextField *)tf withMessage:(NSString*)message{
    if (![tf.text.stringByTrim isEqualToString:@""]&&[tf.text isNotBlank]) {
        return YES;
    }else{
        [MBProgressHUD showError:message ToView:self];
        return NO;
    }
}
-(BOOL)validateCode{
    if (FQIsEmpty(self.realValidateCode)) {
        [MBProgressHUD showError:@"请先获取验证码" ToView:self];
        return NO;
    }
    if ([self.validateCodeTF.text isEqualToString:self.realValidateCode]) {
        return YES;
    }else{
        [MBProgressHUD showError:@"验证码错误" ToView:self];
        return NO;
    }
}
-(BOOL)validatePwdFormat{
    if (self.pwdTF.text.length>=6&&self.pwdTF.text.length<=12) {
        return YES;
    }else{
        [MBProgressHUD showError:@"密码长度必须为6-12位" ToView:self];
        return NO;
    }
}

//#pragma mark api delegate
//-(void)chainRequestFinished:(YTKChainRequest *)chainRequest{
//    [_hud setHidden:YES];
//    [self.validateCodeBtn clickNormalChange];
//
//       JGValidateCodeApi *api=(JGValidateCodeApi*)chainRequest.requestArray[1];
//     _hud.label.text=@"获取成功";
//     [_hud hideAnimated:YES afterDelay:0.5];
//       NSString *validateCode=[api validateCode];
//      _realValidateCode=validateCode;
//        NSLog(@"validateCode=%@",validateCode);
//}
//
//-(void)chainRequestFailed:(YTKChainRequest *)chainRequest failedBaseRequest:(YTKBaseRequest *)request{
//   NSString *msg=request.responseJSONObject[@"data"][@"message"];
//    _hud.label.text=msg;
//    [_hud hideAnimated:YES afterDelay:0.5];
//
//    NSLog(@"%@",request.responseJSONObject);
//    JGLog(@"%@",request.responseString);
//}

@end
